﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MediaDefinitionRepository.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MediaDefinitionRepository type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DocProcessingRepository.Repositories
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using DocProcessingRepository.Contexts;
  using DocProcessingRepository.Interfaces;
  using EFRepository;
  using Entities;

  public class MediaDefinitionRepository : BaseEfRepository<MediaDefinition>, IMediaDefinitionRepository
  {
    public MediaDefinitionRepository(String connectionString)
      : base(new IdentityDb(connectionString))
    {
    }

    public PagedResult<MediaDefinitionEntity> GetByManCoDoc(Int32 pageNumber, Int32 numberOfItems, Int32 manCoDocId)
    {
      var mediaDefinitions =
        this.GetEntityListByStoreProcedure<MediaDefinitionEntity>(String.Format("exec GetMediaDefinitionsByManCoDoc '{0}'", manCoDocId));

      var result = new PagedResult<MediaDefinitionEntity>
                     {
                       CurrentPage = pageNumber,
                       ItemsPerPage = numberOfItems,
                       TotalItems = mediaDefinitions.Count(),
                       Results =
                        mediaDefinitions.OrderBy(md => md.ManCoDocID)
                          .Skip((pageNumber - 1) * numberOfItems)
                          .Take(numberOfItems)
                          .ToList(),
                       StartRow = ((pageNumber - 1) * numberOfItems) + 1,
                       EndRow = (((pageNumber - 1) * numberOfItems) + 1) + (numberOfItems - 1)
                     };

      return result;
    }

    public void UpdatePrintFlag(Int32 mediaDefinitionId, Boolean print)
    {
      Int32 flag = print ? 1 : 0;
      this.ExecuteStoredProcedure(String.Format("UpdatePrintFlagForMediaDefinition {0}, {1}", mediaDefinitionId, flag));
    }
  }
}
